<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	Copyright (c) 2012 Mia-Software. All rights reserved. This program and 
	the accompanying materials are made available under the terms of the Eclipse 
	Public License v1.0 which accompanies this distribution, and is available 
	at http://www.eclipse.org/legal/epl-v10.html 
	
	Contributors: 
		Herve ESTEGUET (Mia-Software) - Bug 381352 - [Releng] There must be a project to configure the build by tycho
-->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
	<xsl:output indent="yes" />
	
	<xsl:param name="feature_id" />
	
	<xsl:template match="/">
		<xsl:element name="feature">
			<xsl:attribute name="id">
				<xsl:choose>
					<xsl:when test="feature/@id != ''">
						<xsl:value-of select="concat(substring-before(feature/@id, '.feature'), '.source.feature')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat(substring-before($feature_id, '.feature'), '.source.feature')"/>
					</xsl:otherwise>
				</xsl:choose>				
			</xsl:attribute>
			<xsl:attribute name="label">
				<xsl:value-of select="concat(feature/@label, ' Source')"/>
			</xsl:attribute>
			<xsl:apply-templates select="feature/attribute::*[name()!='id'][name()!='label']"/>
			<xsl:apply-templates select="feature/child::*"/>
		</xsl:element>
	</xsl:template>
	
	<!-- We only accept includes with IDs prefixed by 'org.eclipse.emf.facet'.
	 If matches are found we allow them, and suffix their IDs with '.source.feature'.
	-->
	<xsl:template match="*[name() = 'includes'][starts-with(@id, 'org.eclipse.emf.facet')][not(ends-with(@id, '.source.feature'))]">
		<xsl:element name="includes">
			<xsl:attribute name="id">
				<xsl:value-of select="concat(substring-before(@id, '.feature'), '.source.feature')"/>
			</xsl:attribute>
			<xsl:apply-templates select="attribute::*[name()!='id']"/>		
		</xsl:element>	
	</xsl:template>
	
	<!-- We only accept plug-in with IDS prefixed by 'org.eclipse.emf.facet'.
	  If matches are found we allow them, and suffix their IDs with '.source'.
	 -->
	<xsl:template match="*[name() = 'plugin'][starts-with(@id, 'org.eclipse.emf.facet')]">
		<xsl:element name="plugin">
			<xsl:attribute name="id">
				<xsl:value-of select="concat(@id, '.source')"/>
			</xsl:attribute>
			<xsl:apply-templates select="attribute::*[name()!='id']"/>
		</xsl:element>
	</xsl:template>
	
	<!-- We copy others node -->
	<xsl:template match="*[name()!='includes'][name()!='plugin']|@*">
		<xsl:copy-of select="."/>
	</xsl:template>
</xsl:stylesheet>
